home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / 3dprograms / rayshade-4.0 / libextra / stupidmalloc.c < prev    next >
C/C++ Source or Header  |  1995-02-13  |  710b  |  52 lines

  1. #include <sys/types.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define STEP (1024 * 128)
  6.  
  7. void *
  8. stupid_malloc(size_t size)
  9. {
  10.   static size_t bytes_left = 0;
  11.   static char *buf;
  12.   static size_t step = STEP;
  13.   static char *current;
  14.   void *result;
  15.  
  16.   if (!step) {
  17.     return NULL;
  18.   }
  19.  
  20.   if (step < size) {
  21.     return malloc(size);
  22.   }
  23.  
  24.   if (bytes_left < size) {
  25.     buf = NULL;
  26.     while (!buf && step) {
  27.       buf = malloc(step);
  28.       if (!buf) {
  29.         step >>= 1;
  30.       }
  31.     }
  32.     if (!step) {
  33.       return NULL;
  34.     }else{
  35.       current = buf;
  36.       bytes_left = step;
  37.     }
  38.   }
  39.  
  40.   result = current;
  41.   current += size;
  42.   bytes_left -= size;
  43.  
  44.   return result;
  45. }
  46.  
  47. void
  48. stupid_free(void *p)
  49. {
  50.   return;
  51. }
  52.